﻿package com._public._ui 
{
	import com._public._control.ChildCtrl;
	import com._public._transitions.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author 望月狼 (shch8.com)
	 * @example
	 * 使用回调方法设置菜单树皮肤
	 * var menuList:ScListMenu = new ScListMenu;
			menuList.rootScButton = createRootBut;//主菜单按钮
			menuList.childScButton = createChildBut;//子菜单按钮
			menuList.selScButton = createSelBut//选中时的按钮
			menuList.style.groupSpace=0//子菜单与主菜单的间距
			menuList.menuData = xml//设置菜单的XML数据
			addChild(menuList)
		private function createChildBut():ScButton
		{
			var but:ScButton = new ScButton
			but.style.normalTxt.color = 0xeeeeee
			but.style.overTxt.color = 0xffffff
			but.style.downTxt.color = 0xeeeeee
			return but
		}
		private function createRootBut():ScButton
		{
			var but:ScButton = new ScButton
			return but
		}
		private function createSelBut():ScButton
		{
			var but:ScButton = new ScButton
			return but
		}
		* @param style: 皮肤与样式 注：不支持动态重绘
		* 如
		* menuList.style.groupSpace=0
		* menuList.selFill= 0x444444;
		* 
		* @param selMenu: 另外，还可以动态设置当前选中的菜单，设置当前选中的菜单，使用xml的ID值
		* public function set selMenu(value:String):void
		* 如：menuList.selMenu="about"
		* 
		* @param hideMenu():隐藏当前鼠标划过的子菜单，并且显示当前选中的菜单
		* 
		* @param 菜单格式，设置方式：menuList.menuData = xml
		* <menu title="首页" id="home"/>
			<menu title="关于我们" id="about">
				<item title="企业简介" id="about-t1"/>
				<item title="企业文化" id="about-t2"/>
				<item title="加盟连锁" id="about-t3"/>
				<item title="企业风采" id="about-t4"/>
				<item title="品牌文化" id="about-t5"/>
				<item title="发展历程" id="about-t6"/>
			</menu>
		* 
	 */
	[Event(name = "select", type = "flash.events.Select")]
	public class ScListMenu extends Sprite
	{
		private var _menuData:XML
		public var style:ParamSet = new ParamSet
		public var rootScButton:Function
		public var childScButton:Function
		public var selScButton:Function
		public var overShow:Boolean = true;//滑过时显示子菜单
		private var childMenu:Sprite
		private var childList:XMLList;//选中的列
		public var selectDate:XMLList
		private var selRoot:ScButton//选取的主菜单
		private var overRoot:ScButton//划过的主菜单
		private var selChild:ScButton//选取的子菜单
		private const ROOT_NAME:String = "root"
		private const CHILD_NAME:String = "child"
		private var thenSel:Boolean//当前显示为选取项
		public function ScListMenu() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE,init)
		}
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,init)
			draw()
		}
		/**
		 * 设置当前选中的菜单，使用xml的ID值
		 */
		public function set selMenu(value:String):void
		{
			var item:ScButton
			var rootItem:ScButton
			var itemList:XMLList
			var j:uint
			for (var i:uint; i < _menuData.child("*").length(); i++ ) {
				rootItem = this.getChildByName(ROOT_NAME + i) as ScButton
				if(rootItem){
					if ( rootItem.data.@id == value) {
						showChildMenu(rootItem)
						setSelState(rootItem)
						return
					}
					overRoot=selRoot=rootItem
				
				//===设置子菜单选中状态
					itemList = _menuData.child(i)
					for (j=0;j < itemList.child("*").length(); j++ ) {
					if (itemList.child(j).@id == value) {
						showChildMenu(rootItem)
						item =childMenu.getChildByName(CHILD_NAME + j) as ScButton
						if (item) {
							selChild=item
						}
						showChildMenu(rootItem)
						setSelState(rootItem)
						return
					}
					}
				}
			}
		}
		public function hideMenu():void
		{
			if (overRoot && selRoot)
			if(overRoot.data != selRoot.data)
			showChildMenu(selRoot)
		}
		public function set menuData(value:XML):void
		{
			_menuData = value;
			draw()
		}
		public function get menuData():XML { return _menuData };
		public function draw():void
		{
			if (_menuData == null) return
			ChildCtrl.getInstance().removeAll(this)
			childMenu=null
			if (!(rootScButton is Function)) {
				throw("请先按装ScButton模板，使用回调函数callScButton和childScButton")
				return
			}
			var item:ScButton
			
			for (var i:uint; i < _menuData.child("*").length(); i++ ) {
				item = createItem(_menuData.child(i).@title,rootScButton());
				item.name = ROOT_NAME + i
				item.data=_menuData.child(i)
				eventManage(item,true,true)
				addChild(item)
				}
			reSize()
		}
		private function createItem(str:String,but:ScButton):ScButton
		{
				but.label = str
				if (style.itemWid != 0) but.width = style.itemWid;
				if (style.itemHei != 0) but.height = style.itemHei;
				return but
		}
		private function childClick(e:MouseEvent):void
		{
			var but:ScButton = e.target as ScButton
			if (overRoot != selRoot) {
				selRoot = overRoot;
				setSelState(overRoot)
			}
			setSelState(but)
			selChild=but
			dispatchEvent(new Event(Event.SELECT))
		}
		private function rootClick(e:MouseEvent):void
		{
			var but:ScButton=e.target as ScButton
			if (overShow && e.type == MouseEvent.MOUSE_OVER) {
				overRoot = but
				showChildMenu(but)
				return
				}
			if (e.type == MouseEvent.CLICK ) {
				selRoot = but
				setSelState(but)
				dispatchEvent(new Event(Event.SELECT))
				}
		}
		private function eventManage(but:ScButton,add:Boolean,isRoot:Boolean):void
		{
			if (isRoot) {
				if (add) {
					but.addEventListener(MouseEvent.MOUSE_OVER, rootClick)
					but.addEventListener(MouseEvent.CLICK, rootClick)
					}else{
					but.removeEventListener(MouseEvent.MOUSE_OVER, rootClick)
					but.removeEventListener(MouseEvent.CLICK, rootClick)
					}
			}else {
				if (add) {
					but.addEventListener(MouseEvent.CLICK, childClick)
					}else {
						but.removeEventListener(MouseEvent.CLICK, childClick)
						}
				}
		}
		//设置选中状态
		private function setSelState(oldBut:ScButton,sel:Boolean=true):void
		{
			var par:Sprite = oldBut.parent as Sprite
			if(par==null)return
			if (oldBut.selected == sel) return;
			if(sel&&oldBut.data)selectDate=oldBut.data as XMLList
			var isRoot:Boolean = par == this;
			if (!sel) {//取消选中
				var but:ScButton = createItem(oldBut.label, isRoot?rootScButton():childScButton());
				par.addChild(but)
				eventManage(oldBut,false,isRoot)
				par.removeChild(oldBut)
				eventManage(but,true,isRoot)
				but.name = oldBut.name
				but.data = oldBut.data
				reSize(isRoot)
				return
				}
			if (!(selScButton is Function)) return;
			
			var getBut:ScButton
			for (var i:uint; i < par.numChildren; i++ ) {
				getBut = par.getChildAt(i) as ScButton
				if(getBut)if(getBut.selected)setSelState(getBut,false)
				}
			but = createItem(oldBut.label, selScButton())
			but.data=oldBut.data
			but.name = oldBut.name
			but.style.selAlpha=0
			but.selected = true
			eventManage(oldBut,false,isRoot)
			par.removeChild(oldBut)
			eventManage(but,true,isRoot)
			par.addChild(but)
			reSize(isRoot)
		}
		private function showChildMenu(but:ScButton):void
		{
			if (childMenu == null) {
				childMenu = new Sprite
				addChild(childMenu)
				}else {
					ChildCtrl.getInstance().removeAll(childMenu)
					}
			
			childMenu.x = style.groupSpace
			var setY:Number = but.y - childMenu.y / 2
			if (setY < 0) setY = 0
			Tweener.addTween(childMenu,{y:setY,time:style.speed})
			childList = _menuData.child(uint(but.name.slice(4)))
			var item:ScButton
			for (var i:uint; i < childList.child("*").length(); i++ ) {
				if(selChild){
				if(childList.child(i).@id==selChild.data.@id){
				item = createItem(childList.child(i).@title, selScButton());
				}else {
					item = createItem(childList.child(i).@title, childScButton());
					}
				}else {
					item = createItem(childList.child(i).@title, childScButton());
					}
				item.name = CHILD_NAME + i
				item.data=childList.child(i)
				item.addEventListener(MouseEvent.CLICK,childClick)
				childMenu.addChild(item)
				}
			reSize(false)
		}
		private function reSize(rootSize:Boolean=true):void
		{
			var but:ScButton
			var setY:uint
			var i:uint
			if (rootSize) {
				for (i=0; i < _menuData.child("*").length(); i++ ) {
				but = getChildByName(ROOT_NAME + i) as ScButton
				if (but) {
					but.y = setY
					but.x=0-but.width
					setY+=but.height+style.rootSpace
					}
				}
			}else {
				for (i=0; i < childList.child("*").length(); i++ ) {
				but = childMenu.getChildByName(CHILD_NAME + i) as ScButton
				if (but) {
					but.y = setY
					setY+=but.height+style.childSpace
					}
				}
				}
		}
	}
	
}
import flash.display.Sprite;
import flash.text.TextFormat;
//设置对象
class ParamSet
{
		/*选取时的文字样式*/
		public var selTxt:TextFormat = new TextFormat("宋体", 12, 0x333333, true)
		
		/*选取的背景填充色*/
		public var selFill:uint = 0x444444;
		
		/*选中时的按钮边框色*/
		public var selLine:uint = 0x000000;
		
		/*选中时的按钮皮肤,影片剪辑或Sprite对象*/
		public var selFace:Sprite;

		/*列表的强制宽高*/
		public var itemWid:uint = 0;
		public var itemHei:uint = 0;
		
		/*主菜单间距*/
		public var rootSpace:uint = 5;
		/*子菜单间距*/
		public var childSpace:uint = 5;
		/*子菜单与主菜单的间距*/
		public var groupSpace:uint = 10;
		/*子菜单的缓动速度0~2*/
		public var speed:Number = 0.8;
}